﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using Things.Common;
using LTP.Accounts.Bus;
namespace Things.ThingsWeb.Goods
{
    public partial class Modify : Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    BindGoodsCategory();

                    Guid GoodsID = new Guid(Request.Params["id"]);
                    ShowInfo(GoodsID);
                }
            }
        }

        protected void BindGoodsCategory()
        {
            Things.BLL.GoodsCategory bll = new BLL.GoodsCategory();
            DataSet ds = bll.GetList(" IsDeleted=0 ");

            ddlGoodsCategory.Items.Clear();
            ddlGoodsCategory.Items.Add(new ListItem(" -- 请选择 -- ", "-1"));

            Things.Common.UICommon.BindDDLTree(ddlGoodsCategory, ds.Tables[0], Guid.Empty, "&nbsp;&nbsp;&nbsp;");
        }

        private void ShowInfo(Guid GoodsID)
        {
            Things.BLL.Goods bll = new Things.BLL.Goods();
            Things.Model.Goods model = bll.GetModel(GoodsID);
            this.lblGoodsID.Text = model.GoodsID.ToString();
            this.txtShelfLife.Text = model.ShelfLife;
            this.txtType.Text = model.Type;
            this.txtCount.Text = model.Count.ToString();
            this.txtGoodsName.Text = model.GoodsName;
            this.txtGoodsBrand.Text = model.GoodsBrand;
            this.txtGoodsWeight.Text = model.GoodsWeight;
            this.txtGoodsPackage.Text = model.GoodsPackage;
            Things.Common.UICommon.SetSelectedDDL(ddlGoodsCategory, model.GoodsCategoryID.ToString());
            this.Image1.ImageUrl = model.GoodsImageURL;
            //ShowImage(Image1, 50, 50);
        }


        public void btnSave_Click(object sender, EventArgs e)
        {

            string strErr = "";

            Guid goodsID = Guid.Empty;
            Guid.TryParse(Request.Params["ID"], out goodsID);
            if (goodsID == Guid.Empty)
            {
                strErr += "商品信息有误！\\n";
            }
            if (this.txtShelfLife.Text.Trim().Length == 0)
            {
                strErr += "保质期不能为空！\\n";
            }
            if (this.txtType.Text.Trim().Length == 0)
            {
                strErr += "规格不能为空！\\n";
            }

            if (!PageValidate.IsNumber(txtCount.Text))
            {
                strErr += "数量格式错误！\\n";
            }
            if (this.txtShengChanPiCi.Text.Trim().Length == 0)
            {
                strErr += "生产批次不能为空！\\n";
            }
            if (this.txtGoodsName.Text.Trim().Length == 0)
            {
                strErr += "商品名称不能为空！\\n";
            }
            if (this.txtGoodsBrand.Text.Trim().Length == 0)
            {
                strErr += "商品品牌不能为空！\\n";
            }
            if (this.txtGoodsWeight.Text.Trim().Length == 0)
            {
                strErr += "商品重量不能为空！\\n";
            }
            if (this.txtGoodsPackage.Text.Trim().Length == 0)
            {
                strErr += "商品包装不能为空！\\n";
            }

            byte[] content = Things.Common.UICommon.GetImages(FileUpload1);
            string goodsImageURL = UploadFile(FileUpload1);
           
            Guid goodsCategoryID = Guid.Empty;
            if (this.ddlGoodsCategory.SelectedValue != "-1" && Guid.TryParse(this.ddlGoodsCategory.SelectedValue, out goodsCategoryID) == false)
            {
                strErr += "商品分类有误！\\n";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            Guid GoodsID = new Guid(this.lblGoodsID.Text);
            string ShelfLife = this.txtShelfLife.Text;
            string Type = this.txtType.Text;
            int Count = int.Parse(this.txtCount.Text);
            string GoodsName = this.txtGoodsName.Text;
            string GoodsBrand = this.txtGoodsBrand.Text;
            string GoodsWeight = this.txtGoodsWeight.Text;
            string GoodsPackage = this.txtGoodsPackage.Text;
            Guid GoodsCategoryID = goodsCategoryID;
            byte[] GoodsImage = content;
            string GoodsImageURL = goodsImageURL;

            Things.BLL.Goods bll = new Things.BLL.Goods();
            Things.Model.Goods model = bll.GetModel(goodsID);
            if (model != null)
            {
                model.ShelfLife = ShelfLife;
                model.Type = Type;
                model.Count = Count;
                model.GoodsName = GoodsName;
                model.GoodsBrand = GoodsBrand;
                model.GoodsWeight = GoodsWeight;
                model.GoodsPackage = GoodsPackage;
                model.GoodsCategoryID = GoodsCategoryID;
                model.ShengChanPiCi = this.txtShengChanPiCi.Text.Trim();
                model.ChuCangFangFa = this.txtChuChangFangFa.Text.Trim();
                model.ShengChanXuKeZhengBianHao = this.txtShengChanXuKeZhengBianHao.Text.Trim();
                model.ChanPinBiaoZhunHao = this.txtChanPinBiaoZhunHao.Text.Trim();
                if (string.IsNullOrEmpty(GoodsImageURL) == false)
                {
                    model.GoodsImage = GoodsImage;
                    model.GoodsImageURL = GoodsImageURL;
                }

                bll.Update(model);
                Things.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "list.aspx");
            }
        }


        public void btnCancle_Click(object sender, EventArgs e)
        {
            Response.Redirect("list.aspx");
        }

        public string UploadFile(FileUpload FileUpload1)
        {
            string imageURL = string.Empty;
            try
            {
                string filePath = FileUpload1.PostedFile.FileName;
                string fileName = filePath.Substring(filePath.LastIndexOf("\\") + 1);                
                string serverPath = Server.MapPath("~/images/") + fileName;
                FileUpload1.PostedFile.SaveAs(serverPath);

                imageURL = "/images/" + fileName;
            }
            catch
            {
                imageURL = string.Empty;
            }

            return imageURL;
        }

        public void ShowImage(Image image, int newHeight, int newWidth)
        {
            int oldHeight = Convert.ToInt32(image.Height.Value);
            int oldWidth = Convert.ToInt32(image.Width.Value);

            if (oldWidth > newWidth || oldHeight > newHeight)
            {
                int scale;
                int scale1 = oldWidth / newWidth;
                int scale2 = oldHeight / newHeight;
                if (scale1 > scale2)
                {
                    scale = scale1;
                }
                else
                {
                    scale = scale2;
                }
                image.Width = oldWidth / scale;
                image.Height = oldWidth / scale;
            }

        }
    }
}
